<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <title>新增页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/component/jsoneditor/css/jsoneditor.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body>

        <form class="layui-form" action="">

            <div class="mainBox">
                <div class="main-container mr-5">
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required" >名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="product_name" value="" class="layui-input" lay-verify="product_name">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required" >产品排序</label>
                        <div class="layui-input-block">
                            <input type="text" name="product_sort" value="" class="layui-input" lay-verify="product_sort">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">图片</label>
                        <div class="layui-input-block">
                            <img class="img-3" src=""/>
                            <input type="text" style="display:none" name="product_img" value="" />
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="product_img" permission="app.admin.upload.product_file">
                                <i class="layui-icon layui-icon-upload"></i>上传图片
                            </button>
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="attachment-choose-product_img" permission="app.admin.upload.attachment">
                                <i class="layui-icon layui-icon-align-left"></i>选择图片
                            </button>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">描述</label>
                        <div class="layui-input-block">
                            <input type="text" name="product_des" value="" class="layui-input" lay-verify="product_des">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">价格</label>
                        <div class="layui-input-block">
                            <input type="number" name="product_price" value="" class="layui-input" lay-verify="product_price">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">过期时间</label>
                        <div class="layui-input-block">
                            <input type="text" name="product_out_time" id="product_out_time" value="" class="layui-input">
                        </div>
                    </div>

<!--                    <div class="layui-form-item">-->
<!--                        <label class="layui-form-label required">类型</label>-->
<!--                        <div class="layui-input-block">-->
<!--                            <input type="text" name="product_type" value="" class="layui-input">-->
<!--                        </div>-->
<!--                    </div>-->
                    <div class="layui-form-item">
                        <label class="layui-form-label required">类型</label>
                        <div class="layui-input-block">
                            <div name="product_type" id="product_type" value="" lay-verify="product_type"></div>
                        </div>
                    </div>
<!--                    <div class="layui-form-item">-->
<!--                        <label class="layui-form-label">性别</label>-->
<!--                        <div class="layui-input-block">-->
<!--                            <div name="sex" id="sex" value="1" ></div>-->
<!--                        </div>-->
<!--                    </div>-->
                    <div class="layui-form-item">
                        <label class="layui-form-label ">发行商</label>
                        <div class="layui-input-block">
                            <input type="text" name="product_owner" value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">支持系统</label>
                        <div class="layui-input-block">
                            <input type="text" name="product_support_os" value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">使用说明</label>
                        <div class="layui-input-block">
                            <input type="text" name="product_shows" value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">附加服务</label>
                        <div class="layui-input-block">
                            <input type="text" name="product_extra_service" value="" class="layui-input">
                        </div>
                    </div>

                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit=""
                        lay-filter="save">
                        提交
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        重置
                    </button>
                </div>
            </div>
            
        </form>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/component/jsoneditor/jsoneditor.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>

        <script>

            // 相关接口
            const INSERT_API = "/app/admin/product/insert";
            const QUERY_PRODUCT_TYPE = "/app/admin/product/query_product_type";

            layui.use(["jquery", "xmSelect"], function() {
                layui.$.ajax({
                    url: QUERY_PRODUCT_TYPE,
                    dataType: "json",
                    success: function (res) {
                        if (res.code === 0) {
                            // 处理数据格式
                            let productTypes = res.data.map(function(item) {
                                return {
                                    name: item.product_type, // 使用产品类型作为名称
                                    value: item.id.toString() // 使用ID作为值，并将其转换为字符串 - 因为 类型是可以变更的 id 不会
                                };
                            });

                            layui.xmSelect.render({
                                el: "#product_type",
                                name: "product_type",
                                data: productTypes,
                                radio: true,
                            });
                        } else {
                            layui.popup.failure(res.msg);
                        }
                    }
                });
            });

            // 字段 头像 avatar
            layui.use(["upload", "layer"], function() {
                let input = layui.$("#product_img").prev();
                input.prev().attr("src", input.val());
                layui.$("#attachment-choose-product_img").on("click", function() {
                    parent.layer.open({
                        type: 2,
                        title: "选择附件",
                        content: "/app/admin/upload/attachment?ext=jpg,jpeg,png,gif,bmp",
                        area: ["95%", "90%"],
                        success: function (layero, index) {
                            parent.layui.$("#layui-layer" + index).data("callback", function (data) {
                                input.val(data.url).prev().attr("src", data.url);
                            });
                        }
                    });
                });
                layui.upload.render({
                    elem: "#product_img",
                    url: "/app/admin/upload/product_img",
                    acceptMime: "image/gif,image/jpeg,image/jpg,image/png",
                    field: "__file__",
                    done: function (res) {
                        if (res.code > 0) return layui.layer.msg(res.msg);
                        this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                    }
                });
            });

            // 字段 生日 birthday
            layui.use(["laydate"], function() {
                layui.laydate.render({
                    elem: "#birthday",
                });
            })

            // 字段 登录时间 last_time
            layui.use(["laydate"], function() {
                layui.laydate.render({
                    elem: "#last_time",
                    type: "datetime",
                });
            })

            // 字段 注册时间 join_time
            layui.use(["laydate"], function() {
                layui.laydate.render({
                    elem: "#join_time",
                    type: "datetime",
                });
            })

            // 字段 禁用 status
            layui.use(["form"], function() {
                layui.$("#status").attr("checked", layui.$('input[name="status"]').val() != 0);
                layui.form.render();
                layui.form.on("switch(status)", function(data) {
                    layui.$('input[name="status"]').val(this.checked ? 1 : 0);
                });
            })
            // 字段 生日 birthday
            layui.use(["laydate"], function() {
                layui.laydate.render({
                    elem: "#product_out_time",
                });
            })
            //提交事件
            layui.use(["form", "popup"], function () {
                layui.form.on("submit(save)", function (data) {
                    console.log("输出提交结果：",data.field)
                    if (!data.field.product_name){
                        layer.msg("请输入产品名称")
                        return false
                    }
                    if (!data.field.product_img){
                        layer.msg("请上传产品图片")
                        return false
                    }
                    if (!data.field.product_des){
                        layer.msg("请添加产品描述")
                        return false
                    }
                    if (!data.field.product_des){
                        layer.msg("请添加产品价格")
                        return false
                    }
                    if (!data.field.product_type){
                        layer.msg("请选择产品类型")
                        return false
                    }
                    layui.$.ajax({
                        url: INSERT_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });
            });


        </script>

    </body>
</html>
